package com.ygo.feihua;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.listener.DownloadFileListener;
import cn.bmob.v3.listener.UpdateListener;
import com.ygo.feihua.BmobTable.Zhuti;
import com.ygo.feihua.view.Advertisements;
import java.io.File;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.support.v7.widget.Toolbar;
import cn.bmob.v3.listener.QueryListener;
import cn.bmob.v3.exception.BmobException;
import android.os.Message;
import java.util.List;
import com.ygo.feihua.util.*;


public class ZhutiXiangxi extends AppCompatActivity
{
LayoutInflater inflater;
	private LinearLayout llAdvertiseBoard;
	private TextView zt_xx_dx,zt_xx_user,zt_xx_xc,zt_xx_jj;
	private Button zt_xx_xz;
	BmobFile wj;
	String xx;
	Gongju gj;
	private DialogUtils du;
	String path;
	Toolbar toolbar;
	int ztdx;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.zhuti_xiangxi);
        gj=Gongju.getdx(ZhutiXiangxi.this);
		du=DialogUtils.getdx(this);
        du.dialogj1(false,"","加载中,请稍等");
		
		toolbar=(Toolbar) findViewById(R.id.toolbar);
		gj.cshToolbar(toolbar,"主题信息");
		Intent intent = this.getIntent();
		String id = intent.getStringExtra("id");
		inflater = LayoutInflater.from(ZhutiXiangxi.this);
		initViews(id);
		
	}
	
	private void initViews(final String id) {
		llAdvertiseBoard = (LinearLayout)findViewById(R.id.llAdvertiseBoard);
	
		zt_xx_dx=(TextView) findViewById(R.id.zt_xx_dx);
		zt_xx_user=(TextView) findViewById(R.id.zt_xx_user);
		zt_xx_xc=(TextView) findViewById(R.id.zt_xx_xc);
		zt_xx_jj=(TextView) findViewById(R.id.zt_xx_jj);
		zt_xx_xz=(Button) findViewById(R.id.zt_xx_xz);

		
		
		
		BmobQuery<Zhuti> zt=new BmobQuery<Zhuti>();
		//zt.addWhereEqualTo("vercode",Gongju.ZHUTI_VERCODE);
		zt.include("ztuser");
		zt.getObject(id, new QueryListener<Zhuti>(){

				@Override
				public void done(Zhuti p1, BmobException p2)
				{
					if(p2==null){
						//主题名
						toolbar.setTitle(p1.getztname());
						
						
						ztdx=p1.getztdx();
						
						zt_xx_dx.setText(gj.fileDx(ztdx));
						//主题简介
						zt_xx_jj.setText(p1.getztjie());
						wj=p1.getZtwj();
						xx=p1.getztname();
						//上传主题用户
						zt_xx_user.setText(p1.getztuser().getUsername());
						//下载次数
						zt_xx_xc.setText(p1.getztxiaci()+"次下载");

						du.dis();


						path = gj.PATH_GEN+"ygo助手/主题/"+p1.getztname()+".zip";
						if((new File(path).exists())){
							
							if(new File(path).length()==ztdx){
							
							zt_xx_xz.setText("应用");
					}
						}
						//简介图
						llAdvertiseBoard.addView(new Advertisements(ZhutiXiangxi.this, true,  3000,2).initView((List<Object>)p1.getZtjt()));
						
					}else{
						du.dis();
						gj.show("加载失败"+p2.getMessage());
						finish();
					}
					// TODO: Implement this method
				}
				
			});

			
			
			
			
		zt_xx_xz.setOnClickListener(new OnClickListener(){

				
			
				@Override
				public void onClick(View p1)
				{
				
					if(id!=null){
						
						if((new File(path).exists())){
							if(new File(path).length()==ztdx){
								gj.yyzt(path);
							}else{
								downloadZt(id);
							}
						}else{
                                downloadZt(id);
					}
					
					// TODO: Implement this method
				}}
			});
		
		}
		public void downloadZt(final String id){
			View[] v=du.dialogj(false,"","下载准备中");
			Button b1=(Button) v[0];
			final TextView t1=(TextView) v[1];
			b1.setText("取消");
			b1.setOnClickListener(new OnClickListener(){

					@Override
					public void onClick(View p1)
					{
						wj.cancel();
						gj.show("取消下载");
						du.dis();
						// TODO: Implement this method
					}
				});   
			
			File saveFile = new File(gj.PATH_GEN+"ygo助手/主题/", xx+".zip");
			wj.download(saveFile,new DownloadFileListener() {

					@Override
					public void done(String p1, BmobException p2)
					{
						if(p2==null){
							Zhuti zzt=new Zhuti();
							zzt.increment("ztxiaci");
							zzt.update(id, new UpdateListener(){

									@Override
									public void done(BmobException p1)
									{
										// TODO: Implement this method
									}
								});
							gj.show("下载成功,保存路径:"+p1);
							zt_xx_xz.setText("应用");

							du.dis();

						}else{
							gj.show("下载失败"+p2);
							du.dis();

						}
						// TODO: Implement this method
					}
					@Override
					public void onProgress(Integer value, long newworkSpeed) {
						t1.setText("下载中"+value+"%");
					}

				});
		}
	
}
